package com.itzsj.service.implement;

import com.itzsj.exception.BaseException;
import com.itzsj.mapper.CommonMapper;
import com.itzsj.pojo.dto.LoginDTO;
import com.itzsj.pojo.dto.RegisterDTO;
import com.itzsj.service.CommonService;
import com.itzsj.common.enumeration.LoginType;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class CommonServiceImpl implements CommonService {

    @Resource
    private CommonMapper commonMapper;

    @Override
    public void login(LoginDTO loginDTO) throws RuntimeException {
        LoginType loginType = loginDTO.getLoginType();
        String account = loginDTO.getAccount();
        String password = null;
        if (LoginType.Enterprise_Login.equals(loginType)) {
            password = commonMapper.enterpriseLogin(account);
        } else if (LoginType.Graduate_Login.equals(loginType)) {
            password = commonMapper.graduateLogin(account);
        }else if (LoginType.Admin_Login.equals(loginType)) {
            password = commonMapper.adminLogin(account);
        }
        if (password == null) {
            throw new BaseException("账号不存在");
        } else if (!password.equals(loginDTO.getPassword())) {
            throw new BaseException("密码错误");
        }
    }

    @Override
    public void register(RegisterDTO registerDTO) {
        commonMapper.register(registerDTO);
    }

}
